import time

from flask import request, jsonify
from flask_script import Manager,Server
from flask_migrate import Migrate,MigrateCommand
from app import db,app
from tools.my_jwt import jj
#注册蓝图
from application.views.users.user import user_blue
app.register_blueprint(user_blue)

# 生成管理工具对象
manager = Manager(app)
# 添加服务器运行指令
manager.add_command("runserver",Server(host='0.0.0.0',use_debugger=True))

# 数据库迁移
Migrate(app,db)
# 添加数据库的操作指令
manager.add_command("db",MigrateCommand)

@app.before_request
def before_request():
    # 设置一个白名单
    whiltelist = ['/getcode','/sendSmsCode','/login']
    url = request.path
    print(url)
    if url not in whiltelist:
        # 判断当前访问的url是否在白名单中，如果不在验证token
        try:
            token = request.headers['token']
            # 如果通过验证过期时间
            # 调用jwt封装好的check_token对比
            flag = jj.check_token(token)
            if flag == True:
                # 如果通过验证过期时间
                payload = jj.jwt_decode(token)
                # 获取token
                now = time.time()
                tnow = payload['data']['exp']
                if int(now)-int(tnow)>3600:
                    return jsonify({'code':401,'msg':'token已经过期'})
            else:
                return jsonify({'code':401,'msg':'token被篡改'})
        except:
            return jsonify({'code':401,'msg':'没获取到token'})








#如果是以此脚本作为主脚本程序，就执行
if __name__ == '__main__':
    # manager.run()
    app.run()